<template>
	<view class="add_comm">
		<form @submit="formSubmit" @reset="">
			<view class="add_comm_title">小区信息</view>
			<view class="add_box">
				<view class="add_inner">
					<view class="add_inner_text">小区名称</view>
					<input class="add_inner_input" type="text" v-model="info.residential_quarters_name" name="residential_quarters_name" value="" placeholder="请输入小区名称" />
				</view>
				<view class="add_inner">
					<view class="add_inner_text">省</view>
					<input class="add_inner_input" type="text" v-model="info.province_name" name="province_name" value="" placeholder="请输入小区所属省" @input="onInput1" />
				</view>
				<view class="add_inner">
					<view class="add_inner_text">市</view>
					<input class="add_inner_input" type="text" v-model="info.city_name" name="city_name" value="" placeholder="请输入小区所属市" @input="onInput2"/>
				</view>
				<view class="add_inner">
					<view class="add_inner_text">区（县）</view>
					<input class="add_inner_input" type="text" v-model="info.area_name" name="area_name" value="" placeholder="请输入小区所属区县" @input="onInput3"/>
				</view>
				<view class="add_inner">
					<view class="add_inner_text">小区地址</view>
					<input class="add_inner_input" type="text" v-model="info.residential_quarters_address" name="residential_quarters_address" value="" placeholder="请输入小区地址" @input="onInput4" @blur="getLoc()"/>
				</view>
				<!-- <view class="add_inner">
					<view class="add_inner_text">坐标</view>
					<input class="add_inner_input" type="text" v-model="info.location" name="location" value="" placeholder="自动获取" />
				</view> -->
				<view class="add_inner">
					<view class="add_inner_text">按时功率≤</view>
					<input class="add_inner_input" type="number" v-model="info.ontime_max_power" name="ontime_max_power" value="" placeholder="请输入按时功率" />
				</view>
				<view class="add_inner">
					<view class="add_inner_text">按量功率≤</view>
					<input class="add_inner_input" type="number" v-model="info.amount_max_power" name="amount_max_power" value="" placeholder="请输入按量功率" />
				</view>
				<view class="add_inner">
					<view class="add_inner_text">电费(元/度)</view>
					<input class="add_inner_input" type="text" v-model="info.electricity_fees" name="electricity_fees" value="" placeholder="请输入电费" />
				</view>
				<view class="add_inner">
					<view class="add_inner_text">备注</view>
					<input class="add_inner_input" type="text" v-model="info.remark" name="remark" value="" placeholder="请输入备注" />
				</view>
				<radio-group class="block" @change="jsfcChange">
					<view class="cu-form-group">
						<view class="title">结算方式</view>
						<view class="title">
							<text class="">
								<radio class='blue' :checked="info.agent_settlement_method==0?true:false" style="transform:scale(0.8)" value="0"></radio>
								<text class="margin-left-xs text-sm ">自己转发</text>
							</text>
							<text class="margin-left-xs">
								<radio class='blue' :checked="info.agent_settlement_method==1?true:false" style="transform:scale(0.8)" value="1"></radio>
								<text class="margin-left-xs text-sm">平台转发</text>
							</text>
						</view>
					</view>
				</radio-group>
			</view>
			<view class="add_comm_title">物业信息（按电费分成请输电费原价）</view>
			<view class="add_box">
				<radio-group class="block" @change="wyfcChange">
					<view class="cu-form-group">
						<view class="title">物业分成模式</view>
						<view class="title">
							<text class="margin-left-xs">
								<radio :checked="info.property_sharing_model==0?true:false" class='blue' style="transform:scale(0.8)" value="0"></radio>
								<text class="margin-left-xs text-sm">无</text>
							</text>
							<text class="">
								<radio :checked="info.property_sharing_model==1?true:false" class='blue' style="transform:scale(0.8)" value="1"></radio>
								<text class="margin-left-xs text-sm ">毛利</text>
							</text>
							<text class="margin-left-xs">
								<radio :checked="info.property_sharing_model==2?true:false" class='blue' style="transform:scale(0.8)" value="2"></radio>
								<text class="margin-left-xs text-sm">纯利</text>
							</text>
						</view>
					</view>
					
				</radio-group>
				<view class="add_inner">
					<view class="add_inner_text">分成比例（%）</view>
					<input class="add_inner_input" type="number" maxlength="3" v-model="info.property_sharing_ratio" value="" name="property_sharing_ratio" placeholder="请输入分成比例" />
				</view>
				<view class="add_inner">
					<view class="add_inner_text">物业分成账号</view>
					<input class="add_inner_input" type="number" v-model="info.property_accounts" value="" name="property_accounts" placeholder="请输入物业分成账号" />
				</view>
				<view class="add_inner">
					<view class="add_inner_text">物业备注</view>
					<input class="add_inner_input" type="text" v-model="info.property_remark" name="property_remark" value="" placeholder="请输入备注" />
				</view>
			</view>
			<view class="add_comm_title">代理信息</view>
			<view class="add_box">
				<radio-group class="block" @change="dlfcChange">
					<view class="cu-form-group">
						<view class="title">代理分成模式</view>
						<view class="title">
							<text class="margin-left-xs">
								<radio :checked="info.agent_sharing_mode==0?true:false" class='blue' style="transform:scale(0.8)" value="0"></radio>
								<text class="margin-left-xs text-sm">无</text>
							</text>
							<text class="">
								<radio :checked="info.agent_sharing_mode==1?true:false" class='blue' style="transform:scale(0.8)" value="1"></radio>
								<text class="margin-left-xs text-sm ">毛利</text>
							</text>
							<text class="margin-left-xs">
								<radio :checked="info.agent_sharing_mode==2?true:false" class='blue' style="transform:scale(0.8)" value="2"></radio>
								<text class="margin-left-xs text-sm">纯利</text>
							</text>
						</view>
					</view>
					
				</radio-group>
				<view class="add_inner">
					<view class="add_inner_text">分成比例（%）</view>
					<input class="add_inner_input" type="number" maxlength="3" v-model="info.agent_sharing_ratio" name="agent_sharing_ratio" value="" placeholder="请输入代理分成比例" />
				</view>
				<view class="add_inner">
					<view class="add_inner_text">代理分成账号</view>
					<input class="add_inner_input" type="number" v-model="info.agent_accounts" value="" name="agent_accounts" placeholder="请输入代理分成账号" />
				</view>
				<view class="add_inner">
					<view class="add_inner_text">代理备注</view>
					<input class="add_inner_input" type="text" v-model="info.agent_remark" name="agent_remark" value="" placeholder="请输入代理备注" />
				</view>
			</view>
			<button type="button" class="add_text_btn" form-type="submit">保存</button>
		</form>
	</view>
</template>

<script>
	import '../../../api/api.js'
	export default {
		data() {
			return {
				jsfs:"",
				wyfc:"",
				dlfc:"",
				location:'',
				guid:"",
				info:{
					location:''
				}
			}
		},
		onLoad(options) {
			let guid=options.guid;
			this.guid=guid;
		},
		onShow() {
			if(this.guid !=null && this.guid!=''){
			   this.getdata();
			}
		},
		methods: {
			jsfcChange(e) {
				this.jsfs = e.detail.value
			},
			wyfcChange(e) {
				this.wyfc = e.detail.value
			},
			dlfcChange(e) {
				this.dlfc = e.detail.value
			},
			getdata(){
				let that=this;
				console.log(that.http.baseUrl)
				that.http.tokenreq({
					url: 'residenialquarers/v1/getinfo',
					data:{"residential_quarters_guid":that.guid}
				}).then((res) => {
					console.log("res="+JSON.stringify(res));
					let e=res.data;
					if(e.code==0 && e.success){
						that.info=e.data;
						that.jsfs = e.data.agent_settlement_method;
						console.log("结算方式="+that.jsfs);
						that.wyfc = e.data.property_sharing_model;
						that.dlfc = e.data.agent_sharing_mode;
					}
				})
			},
			onInput1(e) {
			　　this.info.province_name= e.detail.value;  
			} ,
			onInput2(e) {
			　　this.info.city_name= e.detail.value;  
			} ,
			onInput3(e) {
			　　this.info.area_name= e.detail.value;  
			} ,
			onInput4(e) {  
			　　this.info.residential_quarters_address= e.detail.value;  
			} ,	
			getLoc:function(){
	
			   let addre = '中国'+this.info.province_name.trim()+this.info.city_name.trim()+this.info.area_name.trim()+this.info.residential_quarters_address.trim();
			       uni.request({
			                       url: 'http://restapi.amap.com/v3/geocode/geo?key=389880a06e3f893ea46036f030c94700&address='+addre,
			                       success: (res) => {
			                           console.log('get成功');
									   let cc = JSON.stringify(res.data.geocodes);
									   let ccs = eval('('+cc+')')[0];
										   
					for (var key in ccs) {  
						if(key =='location'){
					           this.info.location= ccs[key];  
					        }
									   }
		                       },
			                       fail:(res)=>{
			                           console.log('get失败');
			                           console.log(res.data);
			                       }
			                   });
			   console.log("sfdsgdfgdfhdhghgjhgfh");
			},
			formSubmit: function(e) {
				let formData = e.detail.value;
			    let residential_quarters_name=formData['residential_quarters_name'];
				if(residential_quarters_name.trim()=='' || residential_quarters_name.trim()==null){
					uni.showToast({
						icon: 'none',
					    title: '小区名称不能为空'
					});
					return;
				}
				let province_name=formData['province_name'];
				console.log(province_name);
				
				if(province_name.trim()=='' || province_name.trim()==null){
					uni.showToast({
						icon: 'none',
					    title: '省份不能为空'
					});
					return;
				}
				let city_name=formData['city_name']
				if(city_name.trim()=='' || city_name.trim()==null){
					uni.showToast({
						icon: 'none',
					    title: '城市不能为空'
					});
					return;
				}
				let area_name=formData['area_name']
				if(area_name.trim()=='' || area_name.trim()==null){
					uni.showToast({
						icon: 'none',
					    title: '区（县）不能为空'
					});
					return;
				}
				let residential_quarters_address=formData['residential_quarters_address']
				if(residential_quarters_address.trim()=='' || residential_quarters_address.trim()==null){
					uni.showToast({
						icon: 'none',
					    title: '小区地址不能为空'
					});
					return;
				}
				
				
				
				
				let ontime_max_power=formData['ontime_max_power']
				if(ontime_max_power.trim()=='' || ontime_max_power.trim()==null){
					uni.showToast({
						icon: 'none',
					    title: '按时功率不能为空'
					});
					return;
				}
				let amount_max_power=formData['amount_max_power']
				if(amount_max_power.trim()=='' || amount_max_power.trim()==null){
					uni.showToast({
						icon: 'none',
					    title: '按量功率不能为空'
					});
					return;
				}
				let electricity_fees=formData['electricity_fees']
				if(electricity_fees.trim()=='' || electricity_fees.trim()==null){
					uni.showToast({
						icon: 'none',
					    title: '小区电费不能为空'
					});
					return;
				}
				let remark=formData['remark'];
				console.log("结算方式="+this.jsfs);
				if(this.jsfs==null || this.jsfs==''){
					console.log("结算方式="+this.jsfs);
					uni.showToast({
						icon: 'none',
					    title: '请选择结算方式'
					});
					return;
				}
				let property_sharing_ratio=formData['property_sharing_ratio']
				let property_accounts=formData['property_accounts']
				if(property_accounts.trim()=='' || property_accounts.trim()==null){
					uni.showToast({
						icon: 'none',
					    title: '物业分成账号不能为空'
					});
					return;
				}
				let property_remark=formData['property_remark']//物业备注
				let agent_sharing_ratio=formData['agent_sharing_ratio']//代理分成比例
				let agent_accounts=formData['agent_accounts']
				if(agent_accounts.trim()=='' || agent_accounts.trim()==null){
					uni.showToast({
						icon: 'none',
					    title: '代理分成账号不能为空'
					});
					return;
				}
				let agent_remark=formData['agent_remark']
				let that=this;
				let url="";
				if(that.guid !=null && that.guid!=''){
				   url="residenialquarers/v1/edit"
				}else{
				   url="residenialquarers/v1/add"
				}
				that.http.tokenreq({
					url:url,
					data:{
						"residential_quarters_guid":that.info.residential_quarters_guid,
						"residential_quarters_name":residential_quarters_name,
						"residential_quarters_address":residential_quarters_address,
						"province_name":province_name,
						"city_name":city_name,
						"area_name":area_name,
						"location":that.location,
						"ontime_max_power":ontime_max_power,
						"amount_max_power":amount_max_power,
						"electricity_fees":electricity_fees,
						"remark":remark,
						"agent_settlement_method":that.jsfs,
						"property_sharing_model":that.wyfc,
						"property_sharing_ratio":property_sharing_ratio,
						"property_accounts":property_accounts,
						"property_remark": property_remark,						
						"agent_sharing_mode":that.dlfc,
						"agent_sharing_ratio":agent_sharing_ratio,
						"agent_accounts":agent_accounts,
						"agent_remark": agent_remark,
					}
				}).then((res) => {
					console.log("res="+JSON.stringify(res));
					let e=res.data;
					if(e.code==0 && e.success){
						uni.showModal({
							content: e.message,
							showCancel:false,
							success: function (res) {
								if (res.confirm) {
									uni.redirectTo({
										url:'/pages/community/community/community'
									})
								}
							}
						});
					}else{
						uni.showToast({
							icon: 'none',
						    title: e.message
						});
					}
				})
			},
		}
	}
</script>

<style>

</style>
